
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32_UART_H__
#define __STM32_UART_H__

/**
  * @brief  
  * @note   依赖于STM32CubeMX生成的usart.h文件，主要是用到了STM32CubeMX
  *         生成usart.h头文件中的串口的底层配置函数HAL_UART_MspInit，用户
  *         也可以自己实现HAL_UART_MspInit函数
  *
  */

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "usart.h"


/* 使用的串口 */
#define STM32_LPUART1_ENABLE    1
#define STM32_UART1_ENABLE      0
#define STM32_UART2_ENABLE      0
#define STM32_UART3_ENABLE      0
#define STM32_UART4_ENABLE      0
#define STM32_UART5_ENABLE      0
#define STM32_UART6_ENABLE      0
#define STM32_UART7_ENABLE      1
#define STM32_UART8_ENABLE      1


/* 重定向printf函数到串口 */
#define CUSTOM_PRINTF_UART

/* 根据实际使用串口选择 */
#define DEBUG_UART_HANDLE       hlpuart1


/* 使用定时器更新中断接收串口一帧数据 */
#define USE_TIM_UPDATE_RECEIVE_UART_FRAME  1
/* 使用串口空闲中断接收串口一帧数据 */
#define USE_UART_IDLE_RECEIVE_UART_FRAME   2
/* 使用串口DMA回调事件接收串口一帧数据 */
#define USE_UART_DMA_RECEIVE_UART_FRAME    3

/* 接收串口一帧数据模式 */
#define UART_RECEIVE_FRAME_MODE  USE_UART_DMA_RECEIVE_UART_FRAME

/* 使用D-Cache加速 */
#define UART_MEMORY_DCACHE_ENABLE  1
/* 内存地址 */
#define LPUART1_TRANSFER_BUFFER_ADDR  0x38000000
#define UART1_TRANSFER_BUFFER_ADDR    0x30020000
#define UART2_TRANSFER_BUFFER_ADDR    0x30021000
#define UART3_TRANSFER_BUFFER_ADDR    0x30022000
#define UART4_TRANSFER_BUFFER_ADDR    0x30023000
#define UART5_TRANSFER_BUFFER_ADDR    0x30024000
#define UART6_TRANSFER_BUFFER_ADDR    0x30025000
#define UART7_TRANSFER_BUFFER_ADDR    0x30026000
#define UART8_TRANSFER_BUFFER_ADDR    0x30027000


/* 串口数据传输结构体 */
typedef struct __UART_Transfer_TypeDef
{
#define UART_DATALEN_MAX            1280      // UART缓冲最大长度
  uint8_t  TxData[UART_DATALEN_MAX];          // UART发送缓冲区
  uint16_t TxData_Len;                        // UART发送缓冲实际长度
  uint8_t  RxData[UART_DATALEN_MAX];          // UART接收缓冲区
  uint16_t RxData_Len;                        // UART接收缓冲实际长度
  uint8_t Package_Data_RX_End_Flag;           // UART接收一包数据完成标志
#define UART_RX_PACKAGE_SIZE        1024      // UART接收一包数据长度
  int32_t  Transfer_TX_Status;                // UART发送传输状态
  int32_t  Transfer_RX_Status;                // UART接收传输状态
#define UART_TRANSFER_IDLE          0x00      // UART状态空闲
#define UART_TRANSFER_START         0x01      // UART传输一帧已开始
#define UART_TRANSFER_END           0x02      // UART传输一帧已完成
  uint8_t  DMARxData[UART_DATALEN_MAX];       // UART接收缓冲区
  uint16_t DMARxData_Len;                     // UART接收缓冲实际长度
} UART_Transfer_TypeDef;


/* 串口数据传输结构体 */
#if (STM32_LPUART1_ENABLE == 1)
extern UART_Transfer_TypeDef LPUART1_Transfer;
#endif

#if (STM32_UART1_ENABLE == 1)
extern UART_Transfer_TypeDef UART1_Transfer;
#endif

#if (STM32_UART2_ENABLE == 1)
extern UART_Transfer_TypeDef UART2_Transfer;
#endif

#if (STM32_UART3_ENABLE == 1)
extern UART_Transfer_TypeDef UART3_Transfer;
#endif

#if (STM32_UART4_ENABLE == 1)
extern UART_Transfer_TypeDef UART4_Transfer;
#endif

#if (STM32_UART5_ENABLE == 1)
extern UART_Transfer_TypeDef UART5_Transfer;
#endif

#if (STM32_UART6_ENABLE == 1)
extern UART_Transfer_TypeDef UART6_Transfer;
#endif

#if (STM32_UART7_ENABLE == 1)
extern UART_Transfer_TypeDef UART7_Transfer;
#endif

#if (STM32_UART8_ENABLE == 1)
extern UART_Transfer_TypeDef UART8_Transfer;
#endif


/* 串口常用操作函数 */
int32_t STM32_UART_Init(USART_TypeDef *STM32_UART, UART_HandleTypeDef *huart, uint32_t BaudRate);
int32_t STM32_UART_TransmitData(UART_HandleTypeDef *huart, uint8_t *Data, uint16_t Length);
int32_t STM32_UART_TransmitData_DMA(UART_HandleTypeDef *huart, uint8_t *Data, uint16_t Length);
void STM32_UART_RxCpltCallback(UART_HandleTypeDef *huart);
void STM32_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void STM32_UART_RxCpltCallback_RXNE(UART_HandleTypeDef *huart);
void STM32_UART_RxCpltCallback_IDLE(UART_HandleTypeDef *huart);


#ifdef __cplusplus
}
#endif

#endif

